﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.IO;

namespace Project01
{
    //Dùng để gọi Monster ra
    public sealed class MonsterComponent : Microsoft.Xna.Framework.DrawableGameComponent
    {
        
        //struct dùng để lấy các giá trị về các loại monster sẽ load lên trong khoảng time nào
        struct MT
        {
           public byte Type;
           public byte Time;
        }
        //lấy các giá trị trong file txt sẽ dc load lên
        List<MT> ListLoadMonster = new List<MT>();

        //List Monster
        List<Monster> MonsterList;

        Monster monster;

        SpriteBatch spriteBatch;

        StreamReader MonsterRead;
        //Thời gian monster xuất hiện
        TimeSpan TimeMonster; 


        public MonsterComponent(Game game)
            : base(game)
        {
        }

        public override void Initialize()
        {
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);
            MonsterList = new List<Monster>();
            MonsterRead = new StreamReader("Monster.txt");
            using (MonsterRead)
            {
                while (MonsterRead.Peek() != -1)
                {
                    string doc = MonsterRead.ReadLine();
                    string[] Data = doc.Split('\t');
                    MT abc = new MT();
                    abc.Type = Convert.ToByte(Data[0]);
                    abc.Time = Convert.ToByte(Data[1]);

                    ListLoadMonster.Add(abc);
                }
            }
            MonsterRead.Close();
                base.Initialize();
        }

        protected override void LoadContent()
        {
           


                base.LoadContent();
        }
        private void  LoadContentMonster(GameTime gametime)
        {
            Random ran = new Random();
            TimeMonster += gametime.ElapsedGameTime;
            for(int i=0;i<ListLoadMonster.Count;i++)
            {
                if (TimeMonster >= TimeSpan.FromSeconds(ListLoadMonster[i].Time))
                {
                    switch (ListLoadMonster[i].Type)
                    {
                        case 1:
                            for (int j = 0; j < ran.Next(4, 6); j++)
                            {
                                monster = new MonsterType03(Game,j);
                                MonsterList.Add(monster);

                            }
                            break;
                        case 2:
                            for (int j = 0; j < 1; j++)
                            {
                                monster = new MonsterType02(Game,j);
                                MonsterList.Add(monster);

                            }
                            break;
                        case 3:
                            for (int j = 0; j < ran.Next(5, 6); j++)
                            {
                                monster = new MonsterType01(Game,j);
                                MonsterList.Add(monster);

                            }
                        
                            break;
                        case 4:
                             for (int j = 0; j < 1; j++)
                            {
                                monster = new MonsterType04(Game,j);
                                MonsterList.Add(monster);

                            }
                        
                            break;
                    }
                    ListLoadMonster.RemoveAt(i);
                }
            }
            
        }
        public override void Update(GameTime gameTime)
        {
            LoadContentMonster(gameTime);
            for (int i = 0; i < MonsterList.Count; i++)
            {
                MonsterList[i].Update(gameTime);
                if (MonsterList[i].Active)
                    MonsterList.RemoveAt(i);
            }
           
            base.Update(gameTime);
        }
        public override void Draw(GameTime gameTime)
        {

            spriteBatch.Begin();
            for (int i = 0; i < MonsterList.Count; i++)
            {
                MonsterList[i].Draw(spriteBatch);
            }
            spriteBatch.End();
            base.Draw(gameTime);
        } 
    }
}
